package controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import pojo.Bianji;
import pojo.Tb_user;
import service.UserService;
import dao.BaseDaoImpl;
@Controller
public class YueLanConteoller {
	@Resource
	private UserService uss;
	
	@Resource
	BaseDaoImpl<Tb_user>bd;
	//@RequestMapping(value="/usyuelan",method=RequestMethod.POST)
	@RequestMapping(value="/manage/usyuelan",method=RequestMethod.GET)
	public String findPage(@RequestParam("pagenow")int pagenow,Model m){
		
		List<Tb_user>list=uss.findpage(pagenow);
		int cout=uss.findall().size();
		int size;
		if(cout%10==0)size=cout/10-1;
		else size=(cout/10);
		m.addAttribute("page", pagenow);
		m.addAttribute("user", list);
		m.addAttribute("size", size);
//		System.out.println("有几条："+cout+"  "+size);
//		System.out.println(pagenow);
		return "/manage/yuelanpage";
	}
	@RequestMapping(value="/manage/usbianji",method=RequestMethod.GET)
	public String update(@RequestParam("id")int id,Model m,HttpServletRequest request){
		Tb_user tbu=uss.findbyid(id);
		Cookie[] cookies=request.getCookies();
		//Cookie cookie=cookies[0];
		for (Cookie cookie : cookies) {
			System.out.println("name:"+cookie.getName()+"  cookie:"+cookie.getValue());
		}
		
		m.addAttribute("id",tbu.getId());
		m.addAttribute("name",tbu.getName());
		m.addAttribute("pass",tbu.getPassword());
		m.addAttribute("role",tbu.getRole());
		return "/manage/bianji";
	}
	
	@RequestMapping(value="/manage/bianjiin",method=RequestMethod.POST)
	public String updatein(Bianji us ,Model m){
		Tb_user tbu=new Tb_user();
		tbu.setId(us.getId());
		tbu.setName(us.getName());
		tbu.setPassword(us.getPass());
		tbu.setRole(us.getRole());
		uss.update(tbu);
		return findPage(0, m);
	}
	@RequestMapping(value="/manage/addus",method=RequestMethod.POST)
	public String adduser(Bianji us,Model m){
		Tb_user tbu=new Tb_user();
		tbu.setId(us.getId());
		tbu.setName(us.getName());
		tbu.setPassword(us.getPass());
		tbu.setRole(us.getRole());
		uss.insert(tbu);
		return findPage(0, m);
	}

}
